package com.sywb.chuangyebao.view;

import android.media.Image;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.AlipayWxWithDrawBean;
import com.sywb.chuangyebao.contract.WeiXinWithDrawContract;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.view.dialog.AlertDialog;
import com.sywb.chuangyebao.view.dialog.ConfirmAlertDialog;

import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class WeiXinWithdrawActivity extends ActionbarActivity<WeiXinWithDrawContract.WeiXinWithDrawPresenter> implements WeiXinWithDrawContract.WeiXinWithDrawView{
    @BindView(R.id.cancel_iv)
    ImageView cancelIv;
    @BindView(R.id.et_withraw_amount)
    EditText etWithrawAmount;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.weixin_name_tv)
    TextView weixinNameTv;
    @BindView(R.id.head_iv)
    ImageView headIv;
    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        setTitle(R.string.weixin_withdrawal);
        TextWatcher amountListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s!=null){
                    String amount = etWithrawAmount.getText().toString().trim();
                    if (!TextUtils.isEmpty(amount)){
                        cancelIv.setVisibility(View.VISIBLE);
                        if (Double.parseDouble(amount)>=30){
                            btnConfirm.setClickable(true);
                            btnConfirm.setBackgroundResource(R.drawable.round20_pie_theme);
                        }else {
                            btnConfirm.setClickable(false);
                            btnConfirm.setBackgroundResource(R.drawable.round20_pie_gray);
                        }
                    }else {
                        //TODO:金额少于0提示
                        btnConfirm.setClickable(false);
                        btnConfirm.setBackgroundResource(R.drawable.round20_pie_gray);
                        cancelIv.setVisibility(View.GONE);
                    }
                }
            }
        };
        etWithrawAmount.addTextChangedListener(amountListener);
        btnConfirm.setClickable(false);
        btnConfirm.setBackgroundResource(R.drawable.round20_pie_gray);
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_weixin_withdraw;
    }
    @OnClick({R.id.cancel_rl})
    public void onClick(View view){
        switch (view.getId())
        {
            case R.id.cancel_rl:
                etWithrawAmount.getText().clear();
                break;

        }
    }

    @Override
    public Button getBtnConfirm() {
        return btnConfirm;
    }

    @Override
    public EditText getWithDrawEditText() {
        return etWithrawAmount;
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }
    //判断微信支付宝是否绑定
    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_ALIPAY_WEIXIN_WITH_DRAW_INFO)})
    public void alipayWxIsBound(AlipayWxWithDrawBean alipayWxWithDrawBean) {
        GlideUtils.getRoundImageToUrl(mActivity, alipayWxWithDrawBean.getHeadimgurl(),headIv,R.drawable.head_image);
        weixinNameTv.setText(alipayWxWithDrawBean.getName());
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_WITH_DRAW)})
    public void withDrawSuccess(String data){
        ConfirmAlertDialog alertDialog = ConfirmAlertDialog.newInstance(getResources().getString(R.string.introduce_withdraw),null);
        alertDialog.show(getMyFragmentManager(), "AlipayWithDrawActivity.showConfirmDialog");
        alertDialog.setClickListener(new AlertDialog.OnClickListener() {
            @Override
            public void onClick(int position) {
                RxBus.get().post(Constants.POST_ALIPAY_WEIXIN_WITH_DRAW_INFO,"");
                finish();
            }
        });
    }

    @Override
    public boolean useRxBus() {
        return true;
    }
}
